package com.zbkj.crmeb.cainiao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.taobao.pac.sdk.cp.PacClient;
import com.taobao.pac.sdk.cp.SendSysParams;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.CustomsDeclareInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.DeclareInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.GlobalSaleOrderNotifyRequest;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.OrderAmountInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.OrderItem;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.ReceiverInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.RefunderInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.SenderInfo;
import com.taobao.pac.sdk.cp.dataobject.response.GLOBAL_SALE_ORDER_NOTIFY.GlobalSaleOrderNotifyResponse;

/**
 * 菜鸟相关接口demo
 * @author daigb
 * @date:  2023-01-03 16:19
 */
public class CaiNiaoDemo {
	private static String appkey = "795670";
	private static String secretKey = "3705lcoVJp70304mlz3v8517584RW07F";
	private static String url = "http://link.cainiao.com/gateway/link.do"; // 生产
//	private static String url = "https://prelink.cainiao.com/gateway/link.do"; //联调
	
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		// 请求
		GlobalSaleOrderNotifyRequest globalSaleOrderNotifyRequest = new GlobalSaleOrderNotifyRequest();
		globalSaleOrderNotifyRequest.setOwnerUserId("2212715138606"); // 货主id
		globalSaleOrderNotifyRequest.setExternalOrderCode("FD2023011117422172419"); // ERP订单编码
		globalSaleOrderNotifyRequest.setOrderType("OPEN_COLLECT"); // 订单跨境业务类型
		globalSaleOrderNotifyRequest.setStoreCode("TRAN_STORE_30442235"); // 仓储编码，ERP指定仓库发货时需要传值，编码由菜鸟提供。如不传值则由菜鸟指定仓库发货
		globalSaleOrderNotifyRequest.setExternalShopName("意大利麦吉集货仓"); // 店铺名称
		globalSaleOrderNotifyRequest.setOrderSource("689"); // 渠道编码
		globalSaleOrderNotifyRequest.setOrderCreateTime(sdf.parse("2023-01-11 17:42:21")); // 订单创建时间
		globalSaleOrderNotifyRequest.setOrderPayTime(sdf.parse("2023-01-11 17:42:28")); // 订单支付时间
		globalSaleOrderNotifyRequest.setSaleMode("1"); // 销售模式0-线下 1-线上
		globalSaleOrderNotifyRequest.setServiceId("100100"); // 服务id
		// 收货方信息
		ReceiverInfo receiverInfo = new ReceiverInfo();
		receiverInfo.setCountry("CN"); // 国家
		receiverInfo.setProvince("浙江省"); // 省份
		receiverInfo.setCity("温州市"); // 城市
		receiverInfo.setDistrict("瓯海区"); // 区县
		receiverInfo.setTown("娄桥街道"); // 镇/街道(非必填)
		receiverInfo.setAddress("娄桥街道广丰大厦702"); // 详细地址
		receiverInfo.setName("金豪杰"); // 姓名
		receiverInfo.setContactNo("13989773877"); // 电话
		globalSaleOrderNotifyRequest.setReceiverInfo(receiverInfo);
		// 发件人信息
		SenderInfo senderInfo = new SenderInfo();
		senderInfo.setCountry("IT"); // 国家
		senderInfo.setProvince("Italy"); // 省份
		senderInfo.setCity("Segrate Milano"); // 城市
		senderInfo.setDistrict(""); // 区县
		senderInfo.setTown(""); // 镇/街道(非必填)
		senderInfo.setAddress("Via Londra 1 BETA Trans Cargo Village"); // 详细地址
		senderInfo.setName("FAN JIAQI"); // 姓名
		senderInfo.setContactNo("+33 7 87 80 49 32"); // 电话
		globalSaleOrderNotifyRequest.setSenderInfo(senderInfo);
		// 退货人信息
		RefunderInfo refunderInfo = new RefunderInfo();
		refunderInfo.setCountry("IT"); // 国家
		refunderInfo.setProvince("Italy"); // 省份
		refunderInfo.setCity("Segrate Milano"); // 城市
		refunderInfo.setDistrict(""); // 区县
		refunderInfo.setTown(""); // 镇/街道(非必填)
		refunderInfo.setAddress("Via Londra 1 BETA Trans Cargo Village"); // 详细地址
		refunderInfo.setName("FAN JIAQI"); // 姓名
		refunderInfo.setContactNo("+33 7 87 80 49 32"); // 电话
		globalSaleOrderNotifyRequest.setRefunderInfo(refunderInfo);
		// 订单商品信息
		List<OrderItem> orderItemList = new ArrayList<>();
		OrderItem item = new OrderItem();
		item.setItemId("691615288446"); // 菜鸟货品ID
		item.setItemQuantity(1); // 商品数量
		// 附加信息
		DeclareInfo declareInfo = new DeclareInfo();
		declareInfo.setConsumptionTax(0L); // 消费税金额
		declareInfo.setCustomsTax(0L); // 商品关税
		declareInfo.setTotalTax(0L); // 商品综合税
		declareInfo.setVat(0L); // 增值税
		declareInfo.setItemTotalPrice(100000L); // 商品总价优惠前
		declareInfo.setItemTotalActualPrice(100000L); // 商品总价优惠后
		item.setDeclareInfo(declareInfo);
		
		orderItemList.add(item);
		globalSaleOrderNotifyRequest.setOrderItemList(orderItemList);
		
		// 附加参数
		OrderAmountInfo orderAmountInfo = new OrderAmountInfo();
		orderAmountInfo.setCoupon(0L);
		orderAmountInfo.setConsumptionTax(0L); // 消费税金额
		orderAmountInfo.setPostFee(0L); // 运费
		orderAmountInfo.setVat(0L); // 增值税金额
		orderAmountInfo.setCustomsTax(0L); // 关税金额
		orderAmountInfo.setActualPayment(100000L); // 实付金额
		orderAmountInfo.setTotalTax(0L); // 综合税金额
		orderAmountInfo.setInsurance(0L); // 保险费金额
		orderAmountInfo.setCurrency("CNY"); // 支付币种
		orderAmountInfo.setDutiablePrice(100000L); // 订单完税总价
		globalSaleOrderNotifyRequest.setOrderAmountInfo(orderAmountInfo);
		CustomsDeclareInfo customsDeclareInfo = new CustomsDeclareInfo();
		customsDeclareInfo.setPayOrderId("2033425703"); // 支付单号
		customsDeclareInfo.setPayChannel("HUIFU"); // 支付渠道
		customsDeclareInfo.setBuyerName("金豪杰"); // 姓名
		customsDeclareInfo.setBuyerIDType("1"); // 证件类型
		customsDeclareInfo.setBuyerIDNo("330382199711036615"); // 身份证
		customsDeclareInfo.setBuyerPlatformId("金豪杰"); // 和buyerName一致
		globalSaleOrderNotifyRequest.setCustomsDeclareInfo(customsDeclareInfo);

		// 初始化
		PacClient pacClient = new PacClient(appkey, secretKey, url);
		pacClient.getLogger().setLogEnable(false); // 关闭日志打印
		// 发送请求
		SendSysParams sendSysParams = new SendSysParams();
		sendSysParams.setFromCode("d6c49d19a3aa64508bc9a7e5fc392711");
		GlobalSaleOrderNotifyResponse response = pacClient.send(globalSaleOrderNotifyRequest, sendSysParams);
		
		System.out.println(JSON.toJSONString(response));
		
	}
}
